package com.dxx.cloud.admin.utils;

import com.dxx.cloud.admin.constant.CommonConst;
import com.dxx.cloud.admin.module.request.TimeAreaRequest;
import org.apache.commons.lang3.ObjectUtils;

import java.lang.reflect.Field;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Map;

/**
 * @author duh
 * @create 2020/5/13 14:19

 **/
public class SqlUtils {

    public static final String DATE_FORMAT_1 = "yyyy-MM-dd";
    public static final String DATE_FORMAT_2 = "yyyy-MM-dd HH:mm:ss";
    public static final String DAY_TIME_SPACE = " ";
    public static final String DAY_TIME_BEGIN = "00:00:00";
    public static final String DAY_TIME_END = "23:59:59";

    private static final DateTimeFormatter df = DateTimeFormatter.ofPattern(DATE_FORMAT_1);

    /**
     * 获取当前日期
     * @return
     */
    public static String currentDate(){
        return df.format(LocalDate.now());
    }

    /**
     * 获取当前日期
     * @return
     */
    public static String todayEnd(){
        return df.format(LocalDate.now()) + DAY_TIME_SPACE + DAY_TIME_END;
    }

    /**
     * 获取当前周一
     * @return
     */
    public static String currentMonday(){
        LocalDate monday = LocalDate.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        return df.format(monday);
    }

    /**
     * 获取当前周日
     * @return
     */
    public static String currentSunday(){
        LocalDate sunday = LocalDate.now().with(TemporalAdjusters.nextOrSame( DayOfWeek.SUNDAY));
        return df.format(sunday);
    }

    /**
     * 获取当月初
     * @return
     */
    public static String currentMonthBegin(){
        LocalDate now = LocalDate.now();
        LocalDate monthBegin = now.minusDays(now.getDayOfMonth() - 1);
        return df.format(monthBegin);
    }
    /**
     * 获取当月末
     * @return
     */
    public static String currentMonthEnd(){
        LocalDate now = LocalDate.now();
        LocalDate monthEnd = now.plusDays(now.lengthOfMonth() - now.getDayOfMonth());
        return df.format(monthEnd);
    }
    /**
     * 处理筛选日期
     * 注意：其它服务中使用到这个方法，修改时请注意兼容性
     * @param dateType
     * @param beginDate
     * @param endDate
     * @return
     */
    public static void dealSearchDate(Map params,String dateType, String beginDate, String endDate) {
        // 日期筛选类型，字段含义请见QueryStaffSourceRequest中的dateType
        String[] begin_end_date =valueBeginEndDate(dateType, beginDate, endDate);
        params.put("startDate", addDateBegin(begin_end_date[0]));
        params.put("endDate", addDateEnd(begin_end_date[1]));
    }
    public static <T extends TimeAreaRequest> void dealSearchDate(T request, String dateType, String beginDate, String endDate) throws IllegalAccessException {
        String[] begin_end_date =valueBeginEndDate(dateType, beginDate, endDate);
        for (Field field : request.getClass().getSuperclass().getDeclaredFields()) {
            if(field.getName().equals("startDate")){
                field.setAccessible(true);
                field.set(request,addDateBegin(begin_end_date[0]));
            }else if(field.getName().equals("endDate")){
                field.setAccessible(true);
                field.set(request,addDateEnd(begin_end_date[1]));
            }
        }
        handleAreaTime(request);
    }

    private static void handleAreaTime(TimeAreaRequest request){
        if(!CommonConst.DEFAULT_TIME_AREA.equals(request.getTimeArea())){
            Integer areaDiff = CommonConst.DEFAULT_TIME_AREA - request.getTimeArea();
            request.setStartDate(LocalTimeUtils.hoursLater(request.getStartDate(),areaDiff));
            request.setEndDate(LocalTimeUtils.hoursLater(request.getEndDate(),areaDiff));
        }
    }
    /**
     * 设置开始日期和结束日期
     * @param dateType
     * @param beginDate
     * @param endDate
     * @return
     */
    private static String[] valueBeginEndDate(String dateType, String beginDate, String endDate) {
        // 日期筛选类型，字段含义请见QueryStaffSourceRequest中的dateType
        String[] begin_end_date = new String[]{beginDate,endDate};
        if (ObjectUtils.isNotEmpty(dateType)) {
            switch (dateType) {
                case "0":
                    break;
                case "1":
                    begin_end_date[0] = currentDate();
                    begin_end_date[1] = currentDate();
                    break;
                case "2":
                    begin_end_date[0] = currentMonday();
                    begin_end_date[1] = currentSunday();
                    break;
                case "3":
                    begin_end_date[0] = currentMonthBegin();
                    begin_end_date[1] = currentMonthEnd();
                    break;
                default:
                    break;
            }
        }
        return begin_end_date;
    }

    public static String addDateBegin(String date){
        String date2 = null;
        if(ObjectUtils.isNotEmpty(date)){
            if(date.length() == DATE_FORMAT_1.length()){
                date2 = date + DAY_TIME_SPACE + DAY_TIME_BEGIN;
            }else if(date.length() ==DATE_FORMAT_2.length()){
                date2 = date;
            }
        }
        return date2;
    }

    public static String addDateEnd(String date){
        String date2 = null;
        if(ObjectUtils.isNotEmpty(date)){
            if(date.length() == DATE_FORMAT_1.length()){
                date2 = date + DAY_TIME_SPACE + DAY_TIME_END;
            }else if(date.length() ==DATE_FORMAT_2.length()){
                date2 =date;
            }
        }
        return date2;
    }

    public static String likeStr(String likeSearchStr){
        String likeStr = "%";
        if(ObjectUtils.isNotEmpty(likeSearchStr)){
            likeStr = "%" + likeSearchStr + "%";
        }
        return likeStr;
    }

    public static void main(String[] args) {
        System.out.println(currentDate());
        System.out.println(currentMonday());
        System.out.println(currentSunday());
        System.out.println(currentMonthBegin());
        System.out.println(currentMonthEnd());
    }
}
